#include "config.h"
#include "system.h"

			//全局变量初始化
uint32_t	Usart1RightRecvTimes = 0;//测试串口1丢包率用
uint32_t  Usart2SendTimes = 0;//测试串口1丢包率用

void CpuInit() 
{
	//CPU相关设置
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //选择第二组中断优先级
}
void SysInit(void)
{
	CpuInit(); //系统配置信息
	
	//系统外设初始化
	SysTickInit();
	LedGpioInit();
	BeepInit();	
	UsartInit();
	ExtiInit();
//Tim2Init(7200,10000); //Timer中断周期是  [1/(72000000/7200)]*20000  单位S
	Tim3Init();//PA6,CH1 PWM  输出PWM; PA7 CH2 PWM 输出PWM;
	Tim4Init();//PB6; Timer4 CH1 (In Capture 输入捕获)
//Tim1Init();//高级定时器全映射，CH1 CH1N CH2 CH2N 互补PWM波形
	

#if SPWM_EN == 1	
	SPWMInit();
#else
	Tim2Init(7200,10000); //Timer中断周期是  [1/(72000000/7200)]*20000  单位S
	Tim1Init();//高级定时器全映射，CH1 CH1N CH2 CH2N 互补PWM波形	
#endif
	
#if ADC_EN == 1
	Adc1Init();
#endif
	
	
#if AT24CXX_EN == 1
   at24cxx_init();
#endif

#if LCD_EN == 1
	 Lcd_Init();			//初始化OLED  
	 LCD_Clear(WHITE);
	 BACK_COLOR=WHITE;
#endif
	DacInit();
	TaskInit();
	IwdgInit(4, 1250);//64分频，每秒625次，重载值1250次，2s
}

//#define ATaskPeriod 200
//#define BTaskPeriod 300
//#define CTaskPeriod 1000
//#define USARTControlBeepTaskPeriod 20



TaskStruct Task[]={
										{IwdgTask,1000},
	                  {ATask,200},
	                  {BTask,300}, 
										{CTask,500}, 
										{USARTControlBeepTask,20},
										{AdcTask,500}
									
										
										//{任务函数名,任务调用周期(单位ms)}
	
	
};


void TaskInit()
{
//	ATaskTimer = ATaskPeriod;
//	BTaskTimer = BTaskPeriod;
//	CTaskTimer = CTaskPeriod;
//	USARTControlBeepTimer = USARTControlBeepTaskPeriod;
	

	uint16_t NTask = 0;
	for(NTask = 0;NTask < sizeof(Task)/sizeof(Task[0]);NTask++)
	TaskTimer[NTask] = Task[NTask].TaskPeriod;
	

	
	
#if  AT24CXX_EN == 1
	I2cAT24C02Test( );//视频17 AT24C02测试代码
#endif
	
#if LCD_EN == 1	
	LCDTest();
#endif
	
	DacSetVol(2.5);
	
#if STM32_FLASH_WREN //进行Flash存储测试时，请将STM32_FLASH_WREN 置为1
		// Flash存储测试
	
	unsigned char sBuff[64]="Hello World! Flash";
	unsigned char pBuff[64]="";
	StmInFlash_WriteBytes(0,sBuff,sizeof(sBuff));
	StmInFlash_ReadBytes(0,pBuff,sizeof(sBuff));
	printf("Flash:%s\n",pBuff);
#endif	

}

void IwdgTask(void)
{
	IwdgFeed();
}

void TaskRun(void)
{
//	ATaskRun();
//	BTaskRun();
//	CTaskRun();	
//	USARTControlBeepTask();
	
	uint16_t NTask = 0;
	for(NTask = 0;NTask < sizeof(Task)/sizeof(Task[0]);NTask++)
	if(TaskTimer[NTask] == 0)
	{
	TaskTimer[NTask] = Task[NTask].TaskPeriod;
	(Task[NTask].pTask)();
	}
}

void	ATask(void) //A 
{
	static uint32 cnt;
	
//	if(ATaskTimer) return ;
//	ATaskTimer = ATaskPeriod;	
	
	if(++cnt%2 == 0)
	{
		GPIO_SetBits(GPIOA, GPIO_Pin_0 );
		cnt = 0;
	}
	else
		GPIO_ResetBits(GPIOA, GPIO_Pin_0);
	


}
void	BTask(void) //B
{	
	static uint32 cnt;
//	if(BTaskTimer) return ;
//	BTaskTimer = BTaskPeriod;
	
	
	if(++cnt%2 == 0)
	{
		GPIO_SetBits(GPIOA, GPIO_Pin_1 );
		cnt = 0;
	}
	else
		GPIO_ResetBits(GPIOA, GPIO_Pin_1);

}

uint8_t Usart1ProcessFinish = 1 ;//测试串口1丢包率用

void	CTask(void) //C
{	
	//static uint32 cnt;
//	if(CTaskTimer) return ;
//	CTaskTimer = CTaskPeriod;
	
	
		//将串口2发送引脚 [接到]<--->[接到] 串口1 接收引脚 进行串口1接收丢包率测试
	if(Usart2SendTimes == 100)
	{
		if(g_tUsart1RecvFrameLenQueue.unResidue == 0)//等待串口1数据处理结束，再进行丢包率计算，否则当串口1接收缓冲区中的数据未进行处理就直接计算则不准确
		{
			printf("Loss Rate:%.2f%%\n",(1-(float)Usart1RightRecvTimes/Usart2SendTimes)*100);
			Usart1RightRecvTimes = 0;
			Usart2SendTimes = 0;
			Usart1ProcessFinish = 1;
		}
		else
			Usart1ProcessFinish = 0;
	}
	if(Usart1ProcessFinish == 1 )
	{
		UsartSendString(USART2,"BeepToggle");
		Usart2SendTimes ++;
	}
	
#if 0	
  printf("System Total Run time is %f s\n",GetRunTime()/1000.0);
	UsartSendString(USART1,(uint8 *)"CTask is Running.\n--------------------------------\n");
	UsartSendString(USART2,(uint8 *)"USART2 is Sending!\n--------------------------------\n");
	
	TIM_SetCompare1(TIM3, (TIM_GetCapture1(TIM3)+10)%100);
  TIM_SetCompare2(TIM3, (TIM_GetCapture2(TIM3)+10)%100);
	
	
	if(waveCapure.ucCapureFinishFlag == 1)
	{
		printf("Current Frequency = %d HZ\n",waveCapure.ulFrequency);
		waveCapure.ucCapureFinishFlag = 0;
	}
	else 
	{
		printf("Input not Exist Or Capure not Imcomplete");
	}
	
	//UsartSendString(USART3,"USART3 is Sending.\n--------------------------------\n");
#endif

}


void USARTControlBeepTask(void) // USARTControlBeepTask
{
//	if(USARTControlBeepTimer) return ;
//	USARTControlBeepTimer = USARTControlBeepTaskPeriod;
	
	Usart1_RecProcess();
	
}
void AdcTask(void) // USARTControlBeepTask
{
#if ADC_EN == 1
	
		printf("\r\n ADC1 (PC10) value = %.2f V\r\n",(float)ADCConvertedValue[0]/4096 *3.3);
		printf("\r\n ADC1 (PC11) value = %.2f V\r\n",(float)ADCConvertedValue[1]/4096 *3.3);
		printf("\r\n ADC1 (PC12) value = %.2f V\r\n",(float)ADCConvertedValue[2]/4096 *3.3);
		printf("\r\n ADC1 (PC13) value = %.2f V\r\n",(float)ADCConvertedValue[3]/4096 *3.3);

#endif
	
}


